﻿using ScanSnap.Domain;
using ScanSnap.Domain.JSON;
using ScanSnap.Domain.TreeData;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows.Documents;


namespace ScanSnap.Util
{
    public class JsonService
    {
        public static readonly string ANY_ID = "anyId";
        public static readonly string ANY_ID_2 = "anyId2";
        public static readonly string HTTP_POST = "POST";
        public static readonly string HTTP_GET = "GET";
        public static readonly string EMPTY_ITEM_NAME = "選択してください";
        public static readonly string EMPTY_ITEM_ID = "";

        public static SignInJSON SignIn(string userName, string password)
        {

            NameValueCollection vals = new NameValueCollection();
            vals.Add("user.email", userName);
            vals.Add("user.password", password);

            try
            {
                SignInJSON singIn = JsonReader.JsonDeserialize<SignInJSON>(RemoteUrl.SignInUrl, HTTP_POST, vals);

                return singIn;
            }
            catch (WebException e)
            {
                throw e;
            }
        }

        public static List<Patient> PatientsByOrganizationidJSON(string organizationId)
        {
            NameValueCollection vals = new NameValueCollection();
            vals.Add(ANY_ID, organizationId);

                OrganizationPatientJSON orgs = JsonReader.JsonDeserialize<OrganizationPatientJSON>(RemoteUrl.DownloadPatientsByOrganizationIdUrl, HTTP_POST, vals);
                if (orgs.Patients != null && orgs.Patients.Count != 0)
                {
                    List<Patient> PatientList = orgs.Patients;

                    //add please select entry
                    PatientList.Add(new Patient { Id = EMPTY_ITEM_ID, Name = EMPTY_ITEM_NAME });

                    PatientList = PatientList.OrderBy(o => o.Id).ToList();
                    return PatientList;
                }
                else
                {
                    return null;
                }
        }

        public static List<Organization> OrgnizationsByUseridJSON(string userId)
        {
            NameValueCollection vals = new NameValueCollection();
            vals.Add(ANY_ID, userId);

            OrganizationJSON organizations;

            try
            {
                organizations = JsonReader.JsonDeserialize<OrganizationJSON>(RemoteUrl.DownloadOrganizationByUserUrl, HTTP_POST, vals);
            }
            catch(WebException e)
            {
                throw e;
            }

            if (organizations != null && organizations.Organizations != null)
            {

                    List<Organization> organizationList = organizations.Organizations;
                    organizationList.Add(new Organization { Id = EMPTY_ITEM_ID, Name = EMPTY_ITEM_NAME });
                    organizationList = organizationList.OrderBy(o => o.Id).ToList();

                    return organizationList;
            }
            else
                return null;
        }

        public static List<DocumentType> DocumentTypes()
        {
            List<DocumentType> DocumentTypeList;

            try
            {
                DocumentTypeJSON documentTypeJSON = JsonReader.JsonDeserialize<DocumentTypeJSON>(RemoteUrl.DownloadDocumentTypeUrl, HTTP_POST);

                if (documentTypeJSON != null)
                {
                    DocumentTypeList = documentTypeJSON.DocumentTypes;
                    DocumentTypeList.Add(new DocumentType { Id = EMPTY_ITEM_ID, Name = EMPTY_ITEM_NAME });
                    DocumentTypeList = DocumentTypeList.OrderBy(o => o.Id).ToList();
                    return DocumentTypeList;
                }
                else
                    return null;
            }
            catch (WebException e)
            {
                throw e;
            }
        }

        public static List<Document> DocumentNames(string documentTypeId)
        {
            List<Document> DocumentNameList;

            NameValueCollection vals = new NameValueCollection();
            vals.Add(ANY_ID, documentTypeId);

            try
            {
                DocumentNameJSON documentNameJSON = JsonReader.JsonDeserialize<DocumentNameJSON>(RemoteUrl.DownloadDocumentNamesByDocumentTypeId, HTTP_POST, vals);

                if (documentNameJSON != null)
                {
                    DocumentNameList = documentNameJSON.Documents;
                    DocumentNameList.Add(new Document { Id = EMPTY_ITEM_ID, Name = EMPTY_ITEM_NAME });
                    DocumentNameList = DocumentNameList.OrderBy(o => o.Id).ToList();
                    return DocumentNameList;
                }
                else
                    return null;
            }
            catch (WebException e)
            {
                throw e;
            }
        }

        public static TagJSON TagsAndCustomTags(string organizationId ,string documentNameId)
        {

            NameValueCollection vals = new NameValueCollection();
            vals.Add(ANY_ID, organizationId);
            vals.Add(ANY_ID_2, documentNameId);

            try
            {
                 TagJSON tagJson = JsonReader.JsonDeserialize<TagJSON>(RemoteUrl.DownloadTagsCustomTagsByOrganizationIdAndDocumentNameId, HTTP_POST, vals);

                if (tagJson != null)
                {
                    return tagJson;
                }
                else
                    return null;
            }
            catch (WebException e)
            {
                throw e;
            }
        }

        public static TagJSON TagsAndCustomTagsList(string organizationId, IList documentIdList)
        {

            NameValueCollection vals = new NameValueCollection();
            vals.Add(ANY_ID, organizationId);


            foreach (Object obj in documentIdList)
            {
                Document doc = (Document)obj;
                //Debug.WriteLine("Document name   " + doc.Name + "  Doc Id   :  " + doc.Id + "    DocTypeID  :  " + doc.DocumentTypeId);
                vals.Add("documentIdListString", doc.Id);
            }
            //vals.Add(ANY_ID_2, documentNameId);

            try
            {
                TagJSON tagJson = JsonReader.JsonDeserialize<TagJSON>(RemoteUrl.DownloadTagsCustomTagsByOrganizationIdAndDocumentIdList, HTTP_POST, vals);

                if (tagJson != null)
                {
                    return tagJson;
                }
                else
                    return null;
            }
            catch (WebException e)
            {
                throw e;
            }
        }

        public static TagJSON TagsAndCustomTags_2(string organizationId)
        {

            NameValueCollection vals = new NameValueCollection();
            vals.Add(ANY_ID, organizationId);

            try
            {
                TagJSON tagJson = JsonReader.JsonDeserialize<TagJSON>(RemoteUrl.DownloadTagsCustomTagsByOrganizationId, HTTP_POST, vals);

                if (tagJson != null)
                {
                    return tagJson;
                }
                else
                    return null;
            }
            catch (WebException e)
            {
                throw e;
            }
        }

        public static OrganizationTree SearchDocuments4JsonTree(NameValueCollection nvc)
        {
            try
            {
                Debug.WriteLine("URL = = =    " + RemoteUrl.DownloadDocumentSearchJsonTreeData);
                Debug.WriteLine("NVC Count  "+nvc.Count);
                for (int i = 0; i < nvc.Count; i++)
                {
                    Debug.WriteLine("NVC   " + i + "    ( " + nvc.GetKey(i) + "   " + nvc.Get(i));

                }
                OrganizationTree orgTreJson = JsonReader.JsonDeserialize2<OrganizationTree>(RemoteUrl.DownloadDocumentSearchJsonTreeData, HTTP_POST, nvc);

                Debug.WriteLine(" orgTreJson  "+orgTreJson.ToString());
                if (orgTreJson !=null)
                {
                    Debug.WriteLine("orgTreJson.organizationTree  is Not Null");
                    orgTreJson.ToString2();
                    return orgTreJson;
                }
                else
                {
                    Debug.WriteLine("orgTreJson.organizationTree  is Null");
                    return null;
                }
            }
            catch (WebException e)
            {
                throw e;
            }

        }
    }
}